package com.letv.business.flow.album;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.BatteryManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.letv.core.utils.LetvBaseObservable;
import com.letv.core.utils.LetvUtils;

/**
 * 播放相关观察者
 * 
 * @author zhuqiao
 *
 */
public class PlayObservable extends LetvBaseObservable {
    private static final String TAG = PlayObservable.class.getSimpleName();
    /**
     * 每分钟的时间变化
     */
    public static final String ON_TIME_CHANGE = TAG + 1;
    /**
     * 网络连接状态变化
     */
    public static final String ON_NET_CHANGE = TAG + 2;
    /**
     * 电量变化
     */
    public static final String ON_BATTERY_CHANGE = TAG + 3;
    /**
     * 下载状态变化
     */
    public static final String ON_DOWNLOAD_CHANGE = TAG + 4;
    /**
     * 耳机插头
     */
    public static final String ON_HEADSET_PLUG = TAG + 5;
    /**
     * 锁屏
     */
    public static final String ON_SCREEN_OFF = TAG + 6;
    /**
     * 解锁
     */
    public static final String ON_USER_PRESENT = TAG + 7;
    /**
     * 开屏
     */
    public static final String ON_SCREEN_ON = TAG + 8;
//    /**
//     * 格式化所有属性很控件
//     */
    //    public static final String ON_FORMAT = LedimVedioTagBean + 10;
    /**
     * 来电
     */
    public static final String ON_CALL_STATE_RINGING = TAG + 11;
    /**
     * 接听
     */
    public static final String ON_CALL_STATE_OFFHOOK = TAG + 12;
    /**
     * 空闲
     */
    public static final String ON_CALL_STATE_IDLE = TAG + 13;

    /**
     * 下载intent
     */
    public static final String INTENT_DOWNLOAD = "com.letv.android.client.download";

    /**
     * 电池状态
     * 
     * @author zhuqiao
     *
     */
    public static class BatteryStatusNotify {
        public final int batteryStatus;
        public final int batterycurPower;

        public BatteryStatusNotify(int batteryStatus, int batterycurPower) {
            this.batteryStatus = batteryStatus;
            this.batterycurPower = batterycurPower;
        }

    }

    private Context mContext;

    /**
     * 监听网络，电量，时间
     */
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (TextUtils.isEmpty(action)) {
                return;
            }

            if (TextUtils.equals(Intent.ACTION_TIME_TICK, action)) {
                // 监听每分钟的时间变化
                notifyObservers(ON_TIME_CHANGE);
            } else if (TextUtils.equals(ConnectivityManager.CONNECTIVITY_ACTION, action)) {
                // 监听网络连接状态变化
                if (LetvUtils.reflectScreenState(mContext)) {
                    notifyObservers(ON_NET_CHANGE);
                }
            } else if (TextUtils.equals(Intent.ACTION_BATTERY_CHANGED, action)) {
                // 监听电量变化
                int batteryStatus = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);// 获得电池状态
                int level = intent.getExtras().getInt("level", 0);// 获得当前电量
                int scale = intent.getExtras().getInt("scale", 100);// 获得总电量
                int batterycurPower = level * 100 / scale;
                notifyObservers(new BatteryStatusNotify(batteryStatus, batterycurPower));
            } else if (TextUtils.equals(INTENT_DOWNLOAD, action)) {
                // 下载状态变化的回调
                notifyObservers(ON_DOWNLOAD_CHANGE);
            } else if (TextUtils.equals(Intent.ACTION_HEADSET_PLUG, action)) {
                // 耳机插头
                notifyObservers(ON_HEADSET_PLUG);
            } else if (TextUtils.equals(Intent.ACTION_SCREEN_OFF, action)) {
                // 锁屏
                notifyObservers(ON_SCREEN_OFF);
            } else if (TextUtils.equals(Intent.ACTION_SCREEN_ON, action)) {
                // 开屏
                notifyObservers(ON_SCREEN_ON);
            } else if (TextUtils.equals(Intent.ACTION_USER_PRESENT, action)) {
                // 解锁
                notifyObservers(ON_USER_PRESENT);
            } else if (TextUtils.equals("android.intent.action.PHONE_STATE", action)) {
                // 来电
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
                switch (tm.getCallState()) {
                    case TelephonyManager.CALL_STATE_RINGING:
                        // 来电中
                        notifyObservers(ON_CALL_STATE_RINGING);
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        // 接听
                        notifyObservers(ON_CALL_STATE_OFFHOOK);
                        break;
                    case TelephonyManager.CALL_STATE_IDLE:
                        // 挂断
                        notifyObservers(ON_CALL_STATE_IDLE);
                        break;
                }
            }
        }
    };

    public PlayObservable(Context context) {
        mContext = context;

        try {
            IntentFilter filter = new IntentFilter();
            filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            filter.addAction(Intent.ACTION_TIME_TICK);
            filter.addAction(Intent.ACTION_BATTERY_CHANGED);
            filter.addAction(INTENT_DOWNLOAD);
            filter.addAction(Intent.ACTION_HEADSET_PLUG);
            filter.addAction("android.intent.action.PHONE_STATE");
            context.registerReceiver(mBroadcastReceiver, filter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void deleteObservers() {
        super.deleteObservers();
        if (mContext != null) {
            mContext.unregisterReceiver(mBroadcastReceiver);
        }
    }

}
